<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of route
 *
 * @author jpsiffert
 */
class route {
  
  static protected $_routes     = array();
  protected $_oPars             = null;
  protected $_nPars             = null;
  protected $_reRouted          = false;
  
  protected function _doUrltoArray($url){
    $ret = $url;
    if(!is_array($url)){
      if(strstr($url,'?')!==FALSE){
        $url = explode('?', $url);
        $url = $url[1];
      }
      $ret=array();
      foreach (explode('&', $url) as $part){
        $part=  explode('=',$part);
        $ret[$part[0]] = isset($part[1]) ? $part[1] : '';
      }
    }
    
    return $ret;
  }

  protected function __construct($oPars,$nPars) {
    
    if(!is_array($oPars)){
      
    }
    
    
    $this->_oPars     = $this->_doUrltoArray($oPars);
    $this->_nPars     = $this->_doUrltoArray($nPars);
    $this->_reRouted  = false;
  }
  
  public static function addRoute($oPars,$nPars){
    self::$_routes[]=new route($oPars,$nPars);
  }
  
  public static function getRoutes(){
    return self::$_routes;
  }
  
  public static function reRoute(&$get){
    
    $routes = self::getRoutes();
    
    $reRouteCnt = 0;
    $good       = true;
    
    if(is_array($routes) && count($routes)){
      
      /* @var $route route */
      foreach ($routes as $route){
        $oPars = $route->_oPars;
        $nPars = $route->_nPars;
        
        if($good){
          $good = true;

          foreach ($oPars as $key => $value) {
            if(!isset($get[$key]) || $get[$key]!=$value ){
              break(2);
            } else {
              $merge[]=$key;
            }
          }

          if($good){
            foreach ($nPars as $key => $value) {
              $get[$key]=$value;
            }
            $reRouteCnt++;
            $route->_reRouted=true;
          }
        
        }
      }
    }
    return $reRouteCnt;
  }
  
  static function get($what){
    global $_GET;
    return isset($_GET[$what]) ? $_GET[$what]: null;
  }
  
  static function getPage(){
    return self::get('page');
  }
  
}

